package com.chinasoft.service.impl;

import com.chinasoft.common.Result;
import com.chinasoft.domain.Student;
import com.chinasoft.mapper.StudentMapper;
import com.chinasoft.query.StudentQuery;
import com.chinasoft.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service//标记此为一个业务层
public class StudentServiceImpl implements StudentService {
    //业务层要去调用mapper层
    @Autowired
    private StudentMapper studentMapper;
    @Override//重写
    public List<Student> loadAll() {
        return studentMapper.loadAll();
    }

    @Override
    public boolean deleteById(Integer id) {
     return    studentMapper.deleteById(id);
    }

    @Override
    public boolean batchDelete(List<Integer> ids) {
        return studentMapper.batchDelete(ids);
    }

    @Override
    public boolean addStudent(Student student) {
        return studentMapper.addStudent(student);
    }

    @Override
    public boolean updateStudent(Student student) {
       return studentMapper.updateStudent(student);
    }

    /**
     * 登录的方法
     * @param name
     * @param age
     * @return
     */
    @Override
    public Student login(String name, Integer age) {
        //判断是否为空
        if ("".equals(name)||age==null){
            throw new RuntimeException("用户名或密码为空");
        }
        //拿着用户名去查询
        Student student = studentMapper.selectByName(name);
        //如果没有该用户
        if(student==null){//没查出来
            throw new RuntimeException("用户名或密码错误");
        }
        //比对密码
        if(age!=student.getAge()){
            throw new RuntimeException("用户名或密码错误");
        }
        return student;
    }

    @Override
    public Student loadByName(String name) {
        return studentMapper.selectByName(name);
    }

    /**
     * 分页查询
     * @param studentQuery
     * @return
     */
    @Override
    public Result pageQuery(StudentQuery studentQuery) {
        //得到总条数
        Long total = studentMapper.count(studentQuery);
        List<Student> students = studentMapper.queryPage(studentQuery);
        return Result.suc(students,total);
    }
}
